




package com.yinhai.ta404.core.validate.validator;

import com.yinhai.ta404.core.autoconfigure.TaRegexMessageProperties;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import javax.validation.ConstraintValidatorContext;
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;

public class PatternValidator extends AbstractPatternValidator<Object> {
    public PatternValidator() {
    }

    public boolean isValid(Object input, ConstraintValidatorContext context) {
        if (Arrays.stream(this.conditions).filter("notnull"::equalsIgnoreCase).toArray().length > 0) {
            if (input == null || input.toString().trim().isEmpty()) {
                ((ConstraintValidatorContextImpl)context).addMessageParameter("message", "传入的数据为空或空字符串");
                return false;
            }

            if (input.getClass().equals(Object.class)) {
                ((ConstraintValidatorContextImpl)context).addMessageParameter("message", "传入的对象为空或不合法.");
                return false;
            }
        }

        if (input instanceof String) {
            return this.doValidString((String)input, context);
        } else {
            return input instanceof Number ? this.doValidString(input.toString(), context) : true;
        }
    }

    private boolean doValidString(String input, ConstraintValidatorContext context) {
        ConstraintValidatorContextImpl constraintValidatorContext = (ConstraintValidatorContextImpl)context;
        StringBuilder sb = new StringBuilder();
        if (input == null) {
            input = "";
        } else {
            input = input.trim();
        }

        int length = input.length();
        AtomicBoolean result = new AtomicBoolean(true);
        AtomicInteger min = new AtomicInteger(0);
        AtomicInteger max = new AtomicInteger(2147483647);
        Arrays.stream(this.conditions).filter((it) -> {
            return it.toLowerCase().startsWith("max=");
        }).forEach((it) -> {
            this.judgeMinAndMaxValueRule(sb, max, true, it, length);
        });
        Arrays.stream(this.conditions).filter((it) -> {
            return it.toLowerCase().startsWith("min=");
        }).forEach((it) -> {
            this.judgeMinAndMaxValueRule(sb, min, false, it, length);
        });
        String finalInput = input;
        Arrays.stream(this.conditions).filter((it) -> {
            return it.toLowerCase().startsWith("regex=");
        }).forEach((it) -> {
            this.isValidCustomRegex(sb, result, finalInput, it);
        });
        Arrays.stream(this.conditions).filter(this::hasCustomRegex).forEach((it) -> {
            this.isValidConfigRegex(sb, result, finalInput, it);
        });
        result.set(result.get() && length <= max.get() && length >= min.get());
        String s = sb.toString();
        s = s.replaceAll("\\|+", "|");
        if (s.endsWith("|")) {
            s = s.substring(0, s.length() - 1);
        }

        constraintValidatorContext.addMessageParameter("message", s);
        return result.get();
    }

    private void judgeMinAndMaxValueRule(final StringBuilder sb, final AtomicInteger min, final boolean isMax, final String rule, final int length) {
        try {
            min.set(Integer.parseInt(rule.replace("max=", "").replace("min=", "")));
        } catch (NumberFormatException var7) {
        }

        if (isMax) {
            if (length > min.get()) {
                sb.append("允许输入的数据最大长度为:").append(min.get()).append(",当前长度为:").append(length).append("|");
            }
        } else if (length < min.get()) {
            sb.append("允许输入的数据最小长度为:").append(min.get()).append(",当前长度为:").append(length).append("|");
        }

    }

    private void isValidCustomRegex(StringBuilder sb, final AtomicBoolean result, final String input, final String rule) {
        String regex = rule.substring(rule.indexOf(61) + 1);
        boolean b = Pattern.matches(regex, input);
        result.set(result.get() && b);
        if (!b) {
            sb.append("输入的数据格式有误！不满足规则:`").append(rule).append("`!");
        }

    }

    private void isValidConfigRegex(StringBuilder sb, final AtomicBoolean result, final String input, final String rule) {
        TaRegexMessageProperties messageRegex = this.getMessageRegex(rule);
        boolean b = Pattern.matches(messageRegex.getRegex(), input);
        result.set(result.get() && b);
        if (!b) {
            sb.append(messageRegex.getMessage()).append("|");
        }

    }
}
